import request from '@/utils/request'
import { encryptObject } from '@/utils/crypto';

// 定义不同存储类型需要加密的敏感字段
const SENSITIVE_FIELDS = {
  aliyun: ['endpoint', 'keyid', 'keysecret', 'bucketname'],
  qiniu: ['accessKey', 'secretKey', 'bucket', 'domain'],
  tencent: ['secretId', 'secretKey', 'bucket', 'region']
};

// 阿里云OSS配置相关接口
export function getAliyunConfig() {
  return request({
    url: '/storage/aliyun',
    method: 'get'
  });
}

export function saveAliyunConfig(data) {
  const encryptedData = encryptObject(data, SENSITIVE_FIELDS.aliyun);
  // 添加存储类型标识
  encryptedData.storageType = 'aliyun';
  return request({
    url: '/storage/aliyun',
    method: 'post',
    data: encryptedData
  });
}

// 七牛云配置相关接口
export function getQiniuConfig() {
  return request({
    url: '/storage/qiniu',
    method: 'get'
  });
}

export function saveQiniuConfig(data) {
  const encryptedData = encryptObject(data, SENSITIVE_FIELDS.qiniu);
  // 添加存储类型标识
  encryptedData.storageType = 'qiniu';
  return request({
    url: '/storage/qiniu',
    method: 'post',
    data: encryptedData
  });
}

// 腾讯云COS配置相关接口
export function getTencentConfig() {
  return request({
    url: '/storage/tencent',
    method: 'get'
  });
}

export function saveTencentConfig(data) {
  const encryptedData = encryptObject(data, SENSITIVE_FIELDS.tencent);
  // 添加存储类型标识
  encryptedData.storageType = 'tencent';
  return request({
    url: '/storage/tencent',
    method: 'post',
    data: encryptedData
  });
}

// 测试存储配置连接
export function testStorageConnection(type, data) {
  const encryptedData = encryptObject(data, SENSITIVE_FIELDS[type]);
  // 添加存储类型标识
  encryptedData.storageType = type;
  return request({
    url: `/storage/test/${type}`,
    method: 'post',
    data: encryptedData
  });
}

// 1. 获取当前默认存储服务器类型（如：'aliyun'、'qiniu'、'tencent'）
export function getDefaultStorage() {
  return request({
    url: '/storage/default',
    method: 'get'
  });
}

// 2. 保存默认存储服务器类型
export function saveDefaultStorage(storageType) {
  return request({
    url: '/storage/default',
    method: 'post',
    data: {
      storageType: storageType 
    }
  });
}